<template>
	<div class="login-wrap">
		<div class="ms-login-phone">
			<div class="ms-title">欢迎登录系统</div>
			<el-form :model="param" :rules="rules" ref="login" label-width="0px" class="ms-content">
				<el-form-item prop="username">
					<el-input v-model="param.username" placeholder="username" class="user-input">
						<template #prepend>
							<div class="icon-box">
								<img src="../assets/img/icon-user.png" alt="">
							</div>
						</template>
					</el-input>
				</el-form-item>
				<el-form-item prop="password">
					<el-input type="password" placeholder="password" v-model="param.password"
						class="user-input password-input" @keyup.enter="submitForm(login)">
						<template #prepend>
							<div class="icon-box">
								<img src="../assets/img/icon-lock.png" alt="">
							</div>
						</template>
					</el-input>
				</el-form-item>
				<el-checkbox v-model="remPwd" label="记住密码" size="large" />
				<div class="login-btn">
					<el-button type="primary" @click="submitForm(login)">登录</el-button>
				</div>
			</el-form>
		</div>
	</div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue';
import { useTagsStore } from '../store/tags';
import { usePermissStore } from '../store/permiss';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import type { FormInstance, FormRules } from 'element-plus';
import { Login, getMenuList } from "../api/index"

interface LoginInfo {
	username: string;
	password: string;
}

const router = useRouter();
const param = reactive<LoginInfo>({
	username: '',
	password: ''
});
const remPwd = ref(true)
const rules: FormRules = {
	username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
	password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
};
const permiss = usePermissStore();
const login = ref<FormInstance>();
const submitForm = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.validate((valid: boolean) => {
		let formData = new FormData()
		formData.append('username', param.username)
		formData.append('password', param.password)
		Login(formData).then(async (res) => {
			if (res.status == 200) {
				ElMessage.success('登录成功');
				let token = res.data.data;
				//设置token
				window.localStorage.setItem('X-Access-Token', token);
				permiss.setDisabled().then(() => {
					if (permiss.menu[0]) {
            router.push('/entry');
						// router.push(permiss.menu[0].route);
					} else {
						router.push('/entry');
					}
				});
			} else {
				switch (res.response.data.code) {
					case 5005:
						ElMessage({
							message: '账号密码错误',
							type: 'warning',
						})
						break;
					case 5014:
						ElMessage({
							message: '该账号已禁用',
							type: 'warning',
						})
						break;
					default:
						ElMessage({
							message: '登录失败',
							type: 'warning',
						})
						break;
				}
			}
		}).catch((err) => {
			ElMessage({
				message: '登录失败:' + err,
				type: 'warning',
			})
		})
	});
};

//判断网页是否是在PC端打开
//PC登录true,其他登录false
function IsPC(): boolean {
	var userAgentInfo = navigator.userAgent;
	var Agents = new Array("Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod");
	var flag = true;
	for (var v = 0; v < Agents.length; v++) {
		if (userAgentInfo.indexOf(Agents[v]) > 0) { flag = false; break; }
	}
	return flag;
}


const tags = useTagsStore();
tags.clearTags();
</script>

<style lang="less" scoped>
.login-wrap {
	width: 100%;
	height: 100%;
	background-size: 100%;
	display: grid;
	justify-items: stretch;
	grid-template-columns: 65% 35%;
}

.ms-title {
	width: 100%;
	line-height: 50px;
	text-align: center;
	font-weight: 600;
	font-size: 28px;
}

//手机端样式
.ms-login-phone {
	width: 100%;
	padding: 12% 13% 10% 15%;
	border-radius: 5px;
	background: rgba(255, 255, 255, 0.3);
	overflow: hidden;
	box-sizing: border-box;
	position: fixed;
	z-index: 2;
}

.ms-content {
	/* padding: 30px 30px; */
	margin-top: 125px;

}

.login-btn {
	text-align: center;

	button {
		height: 66px !important;
		background: linear-gradient(to right, #FF6E32, #FC9131);
		border-radius: 0px;
		margin-bottom: 0px !important;
		border: 0px;
		margin-top: 9%;
	}

	:deep(.el-button>span) {
		font-size: 19px !important;
		font-weight: 600;
	}
}

.login-btn button {
	width: 100%;
	height: 36px;
	margin-bottom: 10px;
}

.login-tips {
	font-size: 12px;
	line-height: 30px;
	color: #fff;
}

.login-left-bg {
	width: 51%;
	height: 67%;
	position: absolute;
	top: 21%;
	left: 7%;

	img {
		width: 100%;
		height: 100%;
	}
}

.sign-slogan {
	width: 11%;
	height: 100%;

	img {
		width: 100%;
		height: 100%;
	}
}

.sign-text {
	color: #000000;
	font-weight: 600;
	font-size: 40px;
	margin-left: 5px;

	.tip {

		color: #757C7F;
		font-size: 16px;
		font-weight: 600;

		&-text {
			transform: scale(.7);
			display: inline-block;
			position: absolute;
			left: 8%;
			width: fit-content;
		}
	}
}

.user-input {

	width: 100%;
	height: 64px;
	border: 1px solid #EA5B24;
	border-radius: 5px;
	// padding: 14px 0px;
	margin-top: -23%;
	background: #ffffff;
}

.password-input {
	margin-top: 24px;
}

.login-divider {
	margin: 30% 0;
}

.icon-box {
	width: 29px;
	height: 29px;

	img {
		width: 100%;
		height: 100%;
	}
}

:deep(.el-input-group__prepend) {
	background: #ffffff;
}

:deep(.el-input-group__prepend) {
	border-right: 1px solid #DDE0EB;

}

:deep(.el-checkbox__label) {
	color: #9BA2A8;
	font-size: 17px;

	font-weight: 600;

}

:deep(.el-checkbox__input.is-checked+.el-checkbox__label) {
	color: #FF6E32;
}

:deep(.el-divider__text) {
	font-size: 19px;
	font-weight: 600;
	color: #AAB3BD;
}
</style>
